package com.qianfeng.smsplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.webmaster.dao.TBlackListMapper;
import com.qianfeng.smsplatform.webmaster.dto.DataGridResult;
import com.qianfeng.smsplatform.webmaster.dto.QueryDTO;
import com.qianfeng.smsplatform.webmaster.mq.PushBlackCacheChange;
import com.qianfeng.smsplatform.webmaster.pojo.TBlackList;
import com.qianfeng.smsplatform.webmaster.pojo.TBlackListExample;
import com.qianfeng.smsplatform.webmaster.service.BlackService;
import com.qianfeng.smsplatform.webmaster.service.api.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BlackServiceImpl implements BlackService {

    private PushBlackCacheChange pushBlackCacheChange;

    @Autowired
    public void setPushBlackCacheChange(PushBlackCacheChange pushBlackCacheChange) {
        this.pushBlackCacheChange = pushBlackCacheChange;
    }

    @Autowired
    private TBlackListMapper tBlackListMapper;

    @Autowired
    private CacheService cacheService;

    @Override
    public int addBlack(TBlackList tBlackList) {
//        cacheService.saveCache("BLACK:"+tBlackList.getMobile(),"1");
        cacheService.add2set(CacheConstants.CACHE_BLACK_KEY, Arrays.asList(tBlackList.getMobile()));
//        声明一个通道
        MessageChannel messageChannel = pushBlackCacheChange.updateBlackChannel();
//        这个消息只是为了通知
        messageChannel.send(new GenericMessage<>("黑名单添加了一个号码"));

        return tBlackListMapper.insertSelective(tBlackList);
    }

    @Override
    public int delBlack(Long id) {
        TBlackList tBlackList = findById(id);
       /* cacheService.del("BLACK:"+tBlackList.getMobile());*/
        System.out.println("tBlackList.getMobile()"+tBlackList.getMobile());
        cacheService.del(CacheConstants.CACHE_BLACK_KEY,tBlackList.getMobile());

        //        声明一个通道
        MessageChannel messageChannel = pushBlackCacheChange.updateBlackChannel();
//        这个消息只是为了通知
        messageChannel.send(new GenericMessage<>("黑名单删除了一个号码"));
        return tBlackListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateBlack(TBlackList tBlackList) {
        TBlackList tBlackList1 = tBlackListMapper.selectByPrimaryKey(tBlackList.getId());
        int i =  tBlackListMapper.updateByPrimaryKey(tBlackList);
        if(i>0){

            cacheService.del(CacheConstants.CACHE_BLACK_KEY, tBlackList1.getMobile());
            cacheService.add2set(CacheConstants.CACHE_BLACK_KEY, Arrays.asList(tBlackList.getMobile()));

            //        声明一个通道
            MessageChannel messageChannel = pushBlackCacheChange.updateBlackChannel();
//        这个消息只是为了通知
            messageChannel.send(new GenericMessage<>("黑名单修改了一个号码"));
        }
        return i;
    }

    @Override
    public TBlackList findById(Long id) {
        return tBlackListMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TBlackList> findAll() {
        return tBlackListMapper.selectByExample(null);
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        TBlackListExample example = new TBlackListExample();
        String sort = queryDTO.getSort();
        if(!StringUtils.isEmpty(sort)){
            example.setOrderByClause("id");
        }
        List<TBlackList> tBlackLists = tBlackListMapper.selectByExample(example);
        PageInfo<TBlackList> info = new PageInfo<>(tBlackLists);
        long total = info.getTotal();
        DataGridResult result = new DataGridResult(total,tBlackLists);
        return result;
    }

    @Override
    public void sync2RediseBlack() {
        TBlackListExample example = new TBlackListExample();
        List<TBlackList> tBlackListList = tBlackListMapper.selectByExample(example);//查询到所有的数据

        System.out.println(tBlackListList);
//        注释的是 一个黑名单一个key  现在要一个key 和所有黑名单
//        Map<String, Object> map = tBlackListList.stream().map(tBlackList -> CacheConstants.CACHE_PREFIX_BLACK + tBlackList.getMobile())//
//                .collect(Collectors.toMap(String -> String, String::length));
//        cacheService.pipelineOps(map);

        //        现在要一个key 和所有黑名单    以set 存取
//              同步是将所有的数据保存到缓存中 为了防止错乱 先将这个key 里面的数据删除
        cacheService.del(CacheConstants.CACHE_BLACK_KEY);
//
        List<String> numList = tBlackListList.stream().map(TBlackList::getMobile).collect(Collectors.toList());
        System.out.println(numList);
        System.out.println("准备黑名单添加缓存");
        //      添加到缓存中
        cacheService.add2set(CacheConstants.CACHE_BLACK_KEY, numList);




    }
}
